package com.icongyou.enterprise.talent_marking.service.impl;

import com.icongyou.enterprise.common.result.Result;
import com.icongyou.enterprise.talent_marking.entity.vo.SubmissionInfo;
import com.icongyou.enterprise.talent_marking.entity.dto.MarkPotential;
import com.icongyou.enterprise.talent_marking.entity.vo.HighPotentialSubmissions;
import com.icongyou.enterprise.talent_marking.mapper.HighPotentialSubmissionMapper;
import com.icongyou.enterprise.talent_marking.service.HighPotentialSubmissionService;
import io.micrometer.common.util.StringUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class HighPotentialSubmissionServiceImpl implements HighPotentialSubmissionService {

    @Resource
    private HighPotentialSubmissionMapper potentialSubmissionMapper;

    @Override
    public Result markHighPotential(MarkPotential markPotential) {
        String userId = markPotential.getUserId();
        String studentId = markPotential.getStudentId();
        String submissionId = markPotential.getSubmissionId();
        String remark = markPotential.getRemark();
        // 参数验证
        validateMarkParams(userId, studentId, submissionId);

        // 检查是否已标记（防止重复标记）
        if (isSubmissionMarked(userId, submissionId)) {
            return Result.error(401,"该成果已被标记为高潜力");
        }
        // 创建标记记录
        HighPotentialSubmissions mark = new HighPotentialSubmissions();
        mark.setId(UUID.randomUUID().toString());
        mark.setUserId(userId);
        mark.setStudentId(studentId);
        mark.setSubmissionId(submissionId);
        mark.setRemark(remark);
        mark.setMarkedTime(new Date());
        potentialSubmissionMapper.insert(mark);
        return Result.success(mark);
    }

    @Override
    public Result updateRemark(MarkPotential markPotential) {
        String userId = markPotential.getUserId();
        String submissionId = markPotential.getSubmissionId();
        String newRemark = markPotential.getRemark();
        HighPotentialSubmissions mark = potentialSubmissionMapper.selectByUserAndSubmission(userId, submissionId);
        if (mark == null) {
            return Result.error(401,"未找到对应的高潜力标记记录");
        }
        mark.setRemark(newRemark);
        potentialSubmissionMapper.updateById(mark);
        return Result.success(mark);
    }

    @Override
    public Result unmarkHighPotential(MarkPotential markPotential) {
        String userId = markPotential.getUserId();
        String submissionId = markPotential.getSubmissionId();
        potentialSubmissionMapper.deleteByUserAndSubmission(userId, submissionId);
        return Result.success();
    }

    @Override
    public Result getMyPotentialMarks(String userId) {
        List<HighPotentialSubmissions> highPotentialSubmissions = potentialSubmissionMapper.selectByUserId(userId);
        if(highPotentialSubmissions == null || highPotentialSubmissions.isEmpty()){
            return Result.success("暂无高潜力成果标记", Collections.EMPTY_LIST);
        }
        return Result.success(highPotentialSubmissions);
    }

    @Override
    public Result getHighPotentialSubmissionInfo(String userId) {
        List<SubmissionInfo> submissionInfoList = potentialSubmissionMapper.selectSubmissionInfoByUserId(userId);
        if(submissionInfoList == null || submissionInfoList.isEmpty()){
            return Result.success("暂无高潜力成果", Collections.EMPTY_LIST);
        }
        return Result.success(submissionInfoList);
    }

    /**
     * 参数验证
     */
    private void validateMarkParams(String userId, String studentId, String submissionId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(studentId) || StringUtils.isBlank(submissionId)) {
            throw new IllegalArgumentException("用户ID、学生ID和提交ID不能为空");
        }
    }
    
    @Override
    public boolean isSubmissionMarked(String userId, String submissionId) {
        HighPotentialSubmissions highPotentialSubmissions = potentialSubmissionMapper.selectByUserAndSubmission(userId, submissionId);
        return highPotentialSubmissions != null;
    }
}
